﻿using System;
using System.Collections.Generic;
using ConsoleDemo.Data;
using TradeHelp.MaterializedObjectViews;

namespace ConsoleDemo
{
    class Program
    {
        static void Main()
        {
            DataSource.StartStreaming();
            Test3();
            DataSource.Stop();
            Console.ReadLine();
        }

        private static void Test1()
        {
            Process(DataSource.FamilyView, "Family");
        }

        private static void Test2()
        {
            Process(DataSource.ShoeView, "Shoe");
        }

        private static void Test3()
        {
            var temp = from s in new Query<Shoe>(DataSource.ShoeView)
                       where s.Brand == "Puma"
                       select s;
            Process(temp.CreateView(), "Shoe Selection");
        }

        private static void Test4()
        {
            var temp = from s in new Query<Shoe>(DataSource.ShoeView)
                       select new { Shoe = s, Dummy = "10" };
            Process(temp.CreateView(), "Shoe Projection");
        }

        private static void Process<T>(View<T> view, String testName)
        {
            Console.Out.WriteLine(testName);
            Console.Out.WriteLine();

            view.StreamData(Handler);
            Console.ReadLine();
            view.Unsubscribe(Handler);
            var data = view.GetData();
            PrintData(data);
            if(view is IDisposable)
            {
                ((IDisposable)view).Dispose();
            }
        }

        private static void PrintData<T>(IEnumerable<T> data)
        {
            foreach (var d in data)
            {
                Console.WriteLine(d);
            }
        }

        private static void Handler<T>(ViewMessageBurst<T> burst)
        {
            PrintData(burst);
            Console.Out.Flush();
        }
    }
}
